//
//  MenuTuiJianChangJingViewController.m
//  Products_B
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 朋学友. All rights reserved.
//

 // 登录前  //  methodName=SceneList&page=1&size=18&token=&user_id=&version=4.4
 // 登录后 //  methodName=SceneList&page=1&size=18&token=D7E92550CDDFBC7CD3E34BF49259A0B5&user_id=1519948&version=4.4
#import "MenuTuiJianChangJingViewController.h"
#import "MenuTuiJianChangJingModel.h"
#import "MenuTuiJianChangjingCollectionViewCell.h"
#import "MenuTuijianChangjingDetailViewController.h"
@interface MenuTuiJianChangJingViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>


@property (nonatomic, strong)NSMutableDictionary *parDic;
@property (nonatomic, strong)NSString *page;

@property (nonatomic, strong)UIButton *tuijianButton;
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic ,strong)UICollectionView *collectionView;
@end

@implementation MenuTuiJianChangJingViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    
    CGFloat x = (self.navigationController.navigationBar.frame.size.height - 30) / 2;
    _tuijianButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _tuijianButton.frame = CGRectMake(kWidth  / 2, x, 60, 30);
    [_tuijianButton setTitle:@"推荐" forState:UIControlStateNormal];
    [_tuijianButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _tuijianButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _tuijianButton.alpha = 0.6;
    [self.navigationController.navigationBar addSubview:_tuijianButton];
    
    self.modelArray = [NSMutableArray array];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource  =self;
    [self.collectionView registerClass:[MenuTuiJianChangjingCollectionViewCell class] forCellWithReuseIdentifier:@"MenuTuiJianChangjingCollectionViewCell"];
    [self.view addSubview:_collectionView];
    self.page = @"1";
    self.parDic = [@{@"methodName":@"SceneList",@"page":@"1",@"size":@"18",@"version":@"4.4"}mutableCopy];
    [self requestData];
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page = [NSString stringWithFormat:@"%ld",[self.page integerValue]+1];
        self.parDic[@"page"] = self.page;
        [self requestData];
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.modelArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MenuTuiJianChangjingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuTuiJianChangjingCollectionViewCell" forIndexPath:indexPath];
    MenuTuiJianChangJingModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MenuTuijianChangjingDetailViewController *detailVc = [[MenuTuijianChangjingDetailViewController alloc]init];
    MenuTuiJianChangJingModel *model = self.modelArray[indexPath.row];
    detailVc.scene_id = model.scene_id;
    [self.navigationController pushViewController:detailVc animated:YES];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kWidth/3 - 1, 150);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return  1;
}
- (void)requestData{
    
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:self.parDic finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",jsonDic);
        NSArray *array = [MenuTuiJianChangJingModel modelConfigureWithDic:jsonDic];
        for (MenuTuiJianChangJingModel *model in array) {
            [self.modelArray addObject:model];
        }
        
        
        [self.collectionView.mj_footer endRefreshing];
       [self.collectionView reloadData];
    } error:nil];
     
}
#pragma mark - 当push的时候隐藏tabBar
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated{
   self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
